
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 语言结构体 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../part15/" />
    
    
    <link rel="prev" href="../part13/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../part9/">
            
                <a href="../part9/">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../part10/">
            
                <a href="../part10/">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../part11/">
            
                <a href="../part11/">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../part13/">
            
                <a href="../part13/">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.15" data-path="./">
            
                <a href="./">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../part15/">
            
                <a href="../part15/">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../part16/">
            
                <a href="../part16/">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../part17/">
            
                <a href="../part17/">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../part20/">
            
                <a href="../part20/">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../part22/">
            
                <a href="../part22/">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 语言结构体</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x4F53;">Go &#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x4F53;</h1>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x4E2D;&#x6570;&#x7EC4;&#x53EF;&#x4EE5;&#x5B58;&#x50A8;&#x540C;&#x4E00;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4F46;&#x5728;&#x7ED3;&#x6784;&#x4F53;&#x4E2D;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4E3A;&#x4E0D;&#x540C;&#x9879;&#x5B9A;&#x4E49;&#x4E0D;&#x540C;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x7ED3;&#x6784;&#x4F53;&#x662F;&#x7531;&#x4E00;&#x7CFB;&#x5217;&#x5177;&#x6709;&#x76F8;&#x540C;&#x7C7B;&#x578B;&#x6216;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#x6784;&#x6210;&#x7684;&#x6570;&#x636E;&#x96C6;&#x5408;&#x3002;</p>
<p>&#x7ED3;&#x6784;&#x4F53;&#x8868;&#x793A;&#x4E00;&#x9879;&#x8BB0;&#x5F55;&#xFF0C;&#x6BD4;&#x5982;&#x4FDD;&#x5B58;&#x56FE;&#x4E66;&#x9986;&#x7684;&#x4E66;&#x7C4D;&#x8BB0;&#x5F55;&#xFF0C;&#x6BCF;&#x672C;&#x4E66;&#x6709;&#x4EE5;&#x4E0B;&#x5C5E;&#x6027;&#xFF1A;</p>
<ul>
<li>Title &#xFF1A;&#x6807;&#x9898;</li>
<li>Author &#xFF1A; &#x4F5C;&#x8005;</li>
<li>Subject&#xFF1A;&#x5B66;&#x79D1;</li>
<li>ID&#xFF1A;&#x4E66;&#x7C4D;ID</li>
</ul>
<hr>
<h2 id="&#x5B9A;&#x4E49;&#x7ED3;&#x6784;&#x4F53;">&#x5B9A;&#x4E49;&#x7ED3;&#x6784;&#x4F53;</h2>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x7ED3;&#x6784;&#x4F53;&#x5B9A;&#x4E49;&#x9700;&#x8981;&#x4F7F;&#x7528; type &#x548C; struct &#x8BED;&#x53E5;&#x3002;struct &#x8BED;&#x53E5;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x7ED3;&#x6784;&#x4F53;&#x4E2D;&#x6709;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x6210;&#x5458;&#x3002;type &#x8BED;&#x53E5;&#x8BBE;&#x5B9A;&#x4E86;&#x7ED3;&#x6784;&#x4F53;&#x7684;&#x540D;&#x79F0;&#x3002;&#x7ED3;&#x6784;&#x4F53;&#x7684;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> struct_variable_type <span class="hljs-keyword">struct</span> {
    member definition
    member definition
    ...
    member definition
}
</code></pre>
<p>&#x4E00;&#x65E6;&#x5B9A;&#x4E49;&#x4E86;&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;&#xFF0C;&#x5B83;&#x5C31;&#x80FD;&#x7528;&#x4E8E;&#x53D8;&#x91CF;&#x7684;&#x58F0;&#x660E;&#xFF0C;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go">variable_name := structure_variable_type {value1, value2...valuen}
&#x6216;
variable_name := structure_variable_type { key1: value1, key2: value2..., keyn: valuen}
</code></pre>
<p>&#x5B9E;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">type</span> Books <span class="hljs-keyword">struct</span> {
    title   <span class="hljs-keyword">string</span>
    author  <span class="hljs-keyword">string</span>
    subject <span class="hljs-keyword">string</span>
    bookId  <span class="hljs-keyword">int</span>
}


<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7ED3;&#x6784;&#x4F53;</span>
    <span class="hljs-comment">//books := Books{&quot;Go &#x8BED;&#x8A00;&quot;, &quot;www.google.com&quot;, &quot;Go &#x8BED;&#x8A00;&#x6559;&#x7A0B;&quot;, 6495407}</span>
    <span class="hljs-comment">//fmt.Println(books)</span>
    fmt.Println(Books{<span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&quot;</span>, <span class="hljs-string">&quot;www.google.com&quot;</span>, <span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&#x6559;&#x7A0B;&quot;</span>, <span class="hljs-number">6495407</span>})
    <span class="hljs-comment">// &#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; key =&gt; value &#x683C;&#x5F0F;</span>
    <span class="hljs-comment">//books = Books{title: &quot;Go &#x8BED;&#x8A00;&quot;, author: &quot;www.google.com&quot;, subject: &quot;Go &#x8BED;&#x8A00;&#x6559;&#x7A0B;&quot;, bookId: 6495407}</span>
    <span class="hljs-comment">//fmt.Println(books)</span>
    fmt.Println(Books{title: <span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&quot;</span>, author: <span class="hljs-string">&quot;www.google.com&quot;</span>, subject: <span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&#x6559;&#x7A0B;&quot;</span>, bookId: <span class="hljs-number">6495407</span>})
    <span class="hljs-comment">// &#x5FFD;&#x7565;&#x7684;&#x5B57;&#x6BB5;&#x4E3A; 0 &#x6216; &#x7A7A;</span>
    <span class="hljs-comment">//books = Books{title: &quot;Go &#x8BED;&#x8A00;&quot;, author: &quot;www.google.com&quot;}</span>
    <span class="hljs-comment">//fmt.Println(books)</span>
    fmt.Println(Books{title: <span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&quot;</span>, author: <span class="hljs-string">&quot;www.google.com&quot;</span>})
}
</code></pre>
<p>&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">{Go &#x8BED;&#x8A00; www.google.com Go &#x8BED;&#x8A00;&#x6559;&#x7A0B; 6495407}
{Go &#x8BED;&#x8A00; www.google.com Go &#x8BED;&#x8A00;&#x6559;&#x7A0B; 6495407}
{Go &#x8BED;&#x8A00; www.google.com  0}
</code></pre>
<hr>
<h2 id="&#x8BBF;&#x95EE;&#x7ED3;&#x6784;&#x4F53;&#x6210;&#x5458;">&#x8BBF;&#x95EE;&#x7ED3;&#x6784;&#x4F53;&#x6210;&#x5458;</h2>
<p>&#x5982;&#x679C;&#x8981;&#x8BBF;&#x95EE;&#x7ED3;&#x6784;&#x4F53;&#x6210;&#x5458;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x70B9;&#x53F7; <strong><code>.</code></strong> &#x64CD;&#x4F5C;&#x7B26;&#xFF0C;&#x683C;&#x5F0F;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-go">&#x7ED3;&#x6784;&#x4F53;.&#x6210;&#x5458;&#x540D;
</code></pre>
<p>&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#x4F7F;&#x7528; struct &#x5173;&#x952E;&#x5B57;&#x5B9A;&#x4E49;&#xFF0C;&#x5B9E;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">type</span> Books <span class="hljs-keyword">struct</span> {
    title   <span class="hljs-keyword">string</span>
    author  <span class="hljs-keyword">string</span>
    subject <span class="hljs-keyword">string</span>
    bookId  <span class="hljs-keyword">int</span>
}

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> Book1 Books        <span class="hljs-comment">/* &#x58F0;&#x660E; Book1 &#x4E3A; Books &#x7C7B;&#x578B; */</span>
    <span class="hljs-keyword">var</span> Book2 Books        <span class="hljs-comment">/* &#x58F0;&#x660E; Book2 &#x4E3A; Books &#x7C7B;&#x578B; */</span>

    <span class="hljs-comment">/* book 1 &#x63CF;&#x8FF0; */</span>
    Book1.title = <span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&quot;</span>
    Book1.author = <span class="hljs-string">&quot;www.google.com&quot;</span>
    Book1.subject = <span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&#x6559;&#x7A0B;&quot;</span>
    Book1.bookId = <span class="hljs-number">6495407</span>

    <span class="hljs-comment">/* book 2 &#x63CF;&#x8FF0; */</span>
    Book2.title = <span class="hljs-string">&quot;Python &#x6559;&#x7A0B;&quot;</span>
    Book2.author = <span class="hljs-string">&quot;www.google.com&quot;</span>
    Book2.subject = <span class="hljs-string">&quot;Python &#x8BED;&#x8A00;&#x6559;&#x7A0B;&quot;</span>
    Book2.bookId = <span class="hljs-number">6495700</span>

    <span class="hljs-comment">/* &#x6253;&#x5370; Book1 &#x4FE1;&#x606F; */</span>
    fmt.Printf( <span class="hljs-string">&quot;Book 1 title : %s\n&quot;</span>, Book1.title)
    fmt.Printf( <span class="hljs-string">&quot;Book 1 author : %s\n&quot;</span>, Book1.author)
    fmt.Printf( <span class="hljs-string">&quot;Book 1 subject : %s\n&quot;</span>, Book1.subject)
    fmt.Printf( <span class="hljs-string">&quot;Book 1 bookId : %d\n&quot;</span>, Book1.bookId)

    <span class="hljs-comment">/* &#x6253;&#x5370; Book2 &#x4FE1;&#x606F; */</span>
    fmt.Printf( <span class="hljs-string">&quot;Book 2 title : %s\n&quot;</span>, Book2.title)
    fmt.Printf( <span class="hljs-string">&quot;Book 2 author : %s\n&quot;</span>, Book2.author)
    fmt.Printf( <span class="hljs-string">&quot;Book 2 subject : %s\n&quot;</span>, Book2.subject)
    fmt.Printf( <span class="hljs-string">&quot;Book 2 bookId : %d\n&quot;</span>, Book2.bookId)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">Book 1 title : Go &#x8BED;&#x8A00;
Book 1 author : www.google.com
Book 1 subject : Go &#x8BED;&#x8A00;&#x6559;&#x7A0B;
Book 1 bookId : 6495407
Book 2 title : Python &#x6559;&#x7A0B;
Book 2 author : www.google.com
Book 2 subject : Python &#x8BED;&#x8A00;&#x6559;&#x7A0B;
Book 2 bookId : 6495700
</code></pre>
<hr>
<h2 id="&#x7ED3;&#x6784;&#x4F53;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x53C2;&#x6570;">&#x7ED3;&#x6784;&#x4F53;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x53C2;&#x6570;</h2>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x50CF;&#x5176;&#x4ED6;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x4E00;&#x6837;&#x5C06;&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x51FD;&#x6570;&#x3002;&#x5E76;&#x4EE5;&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x7684;&#x65B9;&#x5F0F;&#x8BBF;&#x95EE;&#x7ED3;&#x6784;&#x4F53;&#x53D8;&#x91CF;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">type</span> Books <span class="hljs-keyword">struct</span> {
    title   <span class="hljs-keyword">string</span>
    author  <span class="hljs-keyword">string</span>
    subject <span class="hljs-keyword">string</span>
    bookId  <span class="hljs-keyword">int</span>
}

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> Book1 Books        <span class="hljs-comment">/* &#x58F0;&#x660E; Book1 &#x4E3A; Books &#x7C7B;&#x578B; */</span>
    <span class="hljs-keyword">var</span> Book2 Books        <span class="hljs-comment">/* &#x58F0;&#x660E; Book2 &#x4E3A; Books &#x7C7B;&#x578B; */</span>

    <span class="hljs-comment">/* book 1 &#x63CF;&#x8FF0; */</span>
    Book1.title = <span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&quot;</span>
    Book1.author = <span class="hljs-string">&quot;www.google.com&quot;</span>
    Book1.subject = <span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&#x6559;&#x7A0B;&quot;</span>
    Book1.bookId = <span class="hljs-number">6495407</span>

    <span class="hljs-comment">/* book 2 &#x63CF;&#x8FF0; */</span>
    Book2.title = <span class="hljs-string">&quot;Python &#x6559;&#x7A0B;&quot;</span>
    Book2.author = <span class="hljs-string">&quot;www.google.com&quot;</span>
    Book2.subject = <span class="hljs-string">&quot;Python &#x8BED;&#x8A00;&#x6559;&#x7A0B;&quot;</span>
    Book2.bookId = <span class="hljs-number">6495700</span>

    <span class="hljs-comment">/* &#x6253;&#x5370; Book1 &#x4FE1;&#x606F; */</span>
    printBook(Book1)

    <span class="hljs-comment">/* &#x6253;&#x5370; Book2 &#x4FE1;&#x606F; */</span>
    printBook(Book2)
}

<span class="hljs-keyword">func</span> printBook(book Books) {
    fmt.Printf(<span class="hljs-string">&quot;Book title : %s\n&quot;</span>, book.title)
    fmt.Printf(<span class="hljs-string">&quot;Book author : %s\n&quot;</span>, book.author)
    fmt.Printf(<span class="hljs-string">&quot;Book subject : %s\n&quot;</span>, book.subject)
    fmt.Printf(<span class="hljs-string">&quot;Book bookId : %d\n&quot;</span>, book.bookId)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">Book title : Go &#x8BED;&#x8A00;
Book author : www.google.com
Book subject : Go &#x8BED;&#x8A00;&#x6559;&#x7A0B;
Book bookId : 6495407
Book title : Python &#x6559;&#x7A0B;
Book author : www.google.com
Book subject : Python &#x8BED;&#x8A00;&#x6559;&#x7A0B;
Book bookId : 6495700
</code></pre>
<hr>
<h2 id="&#x7ED3;&#x6784;&#x4F53;&#x6307;&#x9488;">&#x7ED3;&#x6784;&#x4F53;&#x6307;&#x9488;</h2>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x6307;&#x5411;&#x7ED3;&#x6784;&#x4F53;&#x7684;&#x6307;&#x9488;&#x7C7B;&#x4F3C;&#x4E8E;&#x5176;&#x4ED6;&#x6307;&#x9488;&#x53D8;&#x91CF;&#xFF0C;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">var</span> struct_pointer *Books
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9A;&#x4E49;&#x7684;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x5B58;&#x50A8;&#x7ED3;&#x6784;&#x4F53;&#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;&#x3002;&#x67E5;&#x770B;&#x7ED3;&#x6784;&#x4F53;&#x53D8;&#x91CF;&#x5730;&#x5740;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06; <code>&amp;</code> &#x7B26;&#x53F7;&#x653E;&#x7F6E;&#x4E8E;&#x7ED3;&#x6784;&#x4F53;&#x53D8;&#x91CF;&#x524D;&#xFF1A;</p>
<pre><code class="lang-go">struct_pointer = &amp;Book1
</code></pre>
<p>&#x4F7F;&#x7528;&#x7ED3;&#x6784;&#x4F53;&#x6307;&#x9488;&#x8BBF;&#x95EE;&#x7ED3;&#x6784;&#x4F53;&#x6210;&#x5458;&#xFF0C;&#x4F7F;&#x7528; &quot;.&quot; &#x64CD;&#x4F5C;&#x7B26;&#xFF1A;</p>
<pre><code class="lang-go">struct_pointer.title
</code></pre>
<p>&#x63A5;&#x4E0B;&#x6765;&#x8BA9;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x7ED3;&#x6784;&#x4F53;&#x6307;&#x9488;&#x91CD;&#x5199;&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#xFF0C;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">type</span> Books <span class="hljs-keyword">struct</span> {
    title   <span class="hljs-keyword">string</span>
    author  <span class="hljs-keyword">string</span>
    subject <span class="hljs-keyword">string</span>
    bookId  <span class="hljs-keyword">int</span>
}

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> Book1 Books        <span class="hljs-comment">/* &#x58F0;&#x660E; Book1 &#x4E3A; Books &#x7C7B;&#x578B; */</span>
    <span class="hljs-keyword">var</span> Book2 Books        <span class="hljs-comment">/* &#x58F0;&#x660E; Book2 &#x4E3A; Books &#x7C7B;&#x578B; */</span>
    <span class="hljs-keyword">var</span> pointerBook1 *Books
    <span class="hljs-keyword">var</span> pointerBook2 *Books

    pointerBook1 = &amp;Book1
    pointerBook2 = &amp;Book2

    <span class="hljs-comment">/* book 1 &#x63CF;&#x8FF0; */</span>
    Book1.title = <span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&quot;</span>
    Book1.author = <span class="hljs-string">&quot;www.runoob.com&quot;</span>
    Book1.subject = <span class="hljs-string">&quot;Go &#x8BED;&#x8A00;&#x6559;&#x7A0B;&quot;</span>
    Book1.bookId = <span class="hljs-number">6495407</span>

    <span class="hljs-comment">/* book 2 &#x63CF;&#x8FF0; */</span>
    Book2.title = <span class="hljs-string">&quot;Python &#x6559;&#x7A0B;&quot;</span>
    Book2.author = <span class="hljs-string">&quot;www.runoob.com&quot;</span>
    Book2.subject = <span class="hljs-string">&quot;Python &#x8BED;&#x8A00;&#x6559;&#x7A0B;&quot;</span>
    Book2.bookId = <span class="hljs-number">6495700</span>

    <span class="hljs-comment">/* &#x6253;&#x5370; Book1 &#x4FE1;&#x606F; */</span>
    printBook(pointerBook1)

    <span class="hljs-comment">/* &#x6253;&#x5370; Book2 &#x4FE1;&#x606F; */</span>
    printBook(pointerBook2)
}

<span class="hljs-keyword">func</span> printBook(book *Books) {
    fmt.Printf( <span class="hljs-string">&quot;Book title : %s\n&quot;</span>, book.title)
    fmt.Printf( <span class="hljs-string">&quot;Book author : %s\n&quot;</span>, book.author)
    fmt.Printf( <span class="hljs-string">&quot;Book subject : %s\n&quot;</span>, book.subject)
    fmt.Printf( <span class="hljs-string">&quot;Book bookId : %d\n&quot;</span>, book.bookId)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">Book title : Go &#x8BED;&#x8A00;
Book author : www.google.com
Book subject : Go &#x8BED;&#x8A00;&#x6559;&#x7A0B;
Book book_id : 6495407
Book title : Python &#x6559;&#x7A0B;
Book author : www.google.com
Book subject : Python &#x8BED;&#x8A00;&#x6559;&#x7A0B;
Book book_id : 6495700
</code></pre>
<hr>
<h2 id="ps">PS</h2>
<ol>
<li><p>struct &#x7C7B;&#x4F3C;&#x4E8E; java &#x4E2D;&#x7684;&#x7C7B;&#xFF0C;&#x53EF;&#x4EE5;&#x5728; struct &#x4E2D;&#x5B9A;&#x4E49;&#x6210;&#x5458;&#x53D8;&#x91CF;&#x3002;
 &#x8981;&#x8BBF;&#x95EE;&#x6210;&#x5458;&#x53D8;&#x91CF;&#xFF0C;&#x53EF;&#x4EE5;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#xFF1A;</p>
<ul>
<li>&#x901A;&#x8FC7; <code>struct &#x53D8;&#x91CF;.&#x6210;&#x5458;</code> &#x53D8;&#x91CF;&#x6765;&#x8BBF;&#x95EE;&#xFF1B;</li>
<li><p>&#x901A;&#x8FC7; <code>truct &#x6307;&#x9488;.&#x6210;&#x5458;</code> &#x53D8;&#x91CF;&#x6765;&#x8BBF;&#x95EE;&#x3002;</p>
<pre><code class="lang-go">  <span class="hljs-keyword">package</span> main

  <span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

  <span class="hljs-keyword">type</span> Rect <span class="hljs-keyword">struct</span> {
      x, y <span class="hljs-keyword">float64</span>
      width, height <span class="hljs-keyword">float64</span>
  }

  <span class="hljs-keyword">func</span> (r *Rect) Area() <span class="hljs-keyword">float64</span> {
      <span class="hljs-keyword">return</span> r.width * r.height
  }

  <span class="hljs-keyword">func</span> main() {
      rect := Rect{width: <span class="hljs-number">2.5</span>, height: <span class="hljs-number">4</span>}
      area := rect.Area()
      fmt.Println(area)
  }
</code></pre>
</li>
</ul>
</li>
<li><p>&#x7ED3;&#x6784;&#x4F53;&#x4E2D;&#x5C5E;&#x6027;&#x7684;&#x9996;&#x5B57;&#x6BCD;&#x5927;&#x5C0F;&#x5199;&#x95EE;&#x9898;</p>
<ul>
<li>&#x9996;&#x5B57;&#x6BCD;&#x5927;&#x5199;&#x76F8;&#x5F53;&#x4E8E; public</li>
<li><p>&#x9996;&#x5B57;&#x6BCD;&#x5C0F;&#x5199;&#x76F8;&#x5F53;&#x4E8E; private</p>
<p>&#x6CE8;&#x610F;: &#x8FD9;&#x4E2A; public &#x548C; private &#x662F;&#x76F8;&#x5BF9;&#x4E8E;&#x5305;&#xFF08;go &#x6587;&#x4EF6;&#x9996;&#x884C;&#x7684; package &#x540E;&#x9762;&#x8DDF;&#x7684;&#x5305;&#x540D;&#xFF09;&#x6765;&#x8BF4;&#x7684;&#x3002;</p>
<p>&#x5F53;&#x8981;&#x5C06;&#x7ED3;&#x6784;&#x4F53;&#x5BF9;&#x8C61;&#x8F6C;&#x6362;&#x4E3A; JSON &#x65F6;&#xFF0C;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x5C5E;&#x6027;&#x9996;&#x5B57;&#x6BCD;&#x5FC5;&#x987B;&#x662F;&#x5927;&#x5199;&#xFF0C;&#x624D;&#x80FD;&#x6B63;&#x5E38;&#x8F6C;&#x6362;&#x4E3A; JSON&#x3002;</p>
<p>&#x793A;&#x4F8B;&#x4E00;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
  <span class="hljs-string">&quot;encoding/json&quot;</span>
  <span class="hljs-string">&quot;fmt&quot;</span>
)

<span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
  Name <span class="hljs-keyword">string</span>        <span class="hljs-comment">// Name&#x5B57;&#x6BB5;&#x9996;&#x5B57;&#x6BCD;&#x5927;&#x5199;</span>
  age <span class="hljs-keyword">int</span>        <span class="hljs-comment">// age&#x5B57;&#x6BB5;&#x9996;&#x5B57;&#x6BCD;&#x5C0F;&#x5199;</span>
}

<span class="hljs-keyword">func</span> main() {
  person := Person{<span class="hljs-string">&quot;&#x5C0F;&#x660E;&quot;</span>, <span class="hljs-number">18</span>}
  <span class="hljs-comment">// json.Marshal &#x5C06;&#x5BF9;&#x8C61;&#x8F6C;&#x6362;&#x4E3A;json&#x5B57;&#x7B26;&#x4E32;</span>
  <span class="hljs-keyword">if</span> result, err := json.Marshal(&amp;person); err == <span class="hljs-literal">nil</span> {
      fmt.Println(<span class="hljs-keyword">string</span>(result))
  }
}
</code></pre>
<p>&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-json">{<span class="hljs-string">&quot;Name&quot;</span>:<span class="hljs-string">&quot;&#x5C0F;&#x660E;&quot;</span>}
</code></pre>
<p>&#x793A;&#x4F8B;&#x4E8C;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
  Name <span class="hljs-keyword">string</span>        <span class="hljs-comment">// &#x90FD;&#x662F;&#x5927;&#x5199;</span>
  Age <span class="hljs-keyword">int</span>
}
</code></pre>
<p>&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-json">{<span class="hljs-string">&quot;Name&quot;</span>:<span class="hljs-string">&quot;&#x5C0F;&#x660E;&quot;</span>,<span class="hljs-string">&quot;Age&quot;</span>:<span class="hljs-number">18</span>}   <span class="hljs-comment">//&#x4E24;&#x4E2A;&#x5B57;&#x6BB5;&#x90FD;&#x6709;</span>
</code></pre>
<p>&#x90A3;&#x8FD9;&#x6837; JSON &#x5B57;&#x7B26;&#x4E32;&#x4EE5;&#x540E;&#x5C31;&#x53EA;&#x80FD;&#x662F;&#x5927;&#x5199;&#x4E86;&#x4E48;&#xFF1F; &#x5F53;&#x7136;&#x4E0D;&#x662F;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; tag &#x6807;&#x8BB0;&#x8981;&#x8FD4;&#x56DE;&#x7684;&#x5B57;&#x6BB5;&#x540D;&#x3002;</p>
<p>&#x793A;&#x4F8B;&#x4E09;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
  <span class="hljs-string">&quot;encoding/json&quot;</span>
  <span class="hljs-string">&quot;fmt&quot;</span>
  <span class="hljs-string">&quot;time&quot;</span>
)

<span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
  Name <span class="hljs-keyword">string</span>    <span class="hljs-string">`json:&quot;name&quot;`</span>    <span class="hljs-comment">// &#x6807;&#x8BB0;json&#x540D;&#x5B57;&#x4E3A;name</span>
  Age <span class="hljs-keyword">int</span>    <span class="hljs-string">`json:&quot;-&quot;`</span>    <span class="hljs-comment">// &#x6807;&#x8BB0;&#x5FFD;&#x7565;&#x8BE5;&#x5B57;&#x6BB5;</span>
  Time <span class="hljs-keyword">string</span> <span class="hljs-string">`json:&quot;time&quot;`</span>    <span class="hljs-comment">// &#x6807;&#x8BB0;json&#x540D;&#x5B57;&#x4E3A;time</span>
}

<span class="hljs-keyword">func</span> main() {
  person := Person{<span class="hljs-string">&quot;&#x5C0F;&#x660E;&quot;</span>, <span class="hljs-number">18</span>, time.Now().Format(<span class="hljs-string">&quot;2006-01-02 15:04:05&quot;</span>)}
  <span class="hljs-comment">// json.Marshal &#x5C06;&#x5BF9;&#x8C61;&#x8F6C;&#x6362;&#x4E3A;json&#x5B57;&#x7B26;&#x4E32;</span>
  <span class="hljs-keyword">if</span> result, err := json.Marshal(&amp;person); err == <span class="hljs-literal">nil</span> {
      fmt.Println(<span class="hljs-keyword">string</span>(result))
  }
}
</code></pre>
<p>&#x7ED3;&#x679C;</p>
<pre><code class="lang-json">{<span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;&#x5C0F;&#x660E;&quot;</span>,<span class="hljs-string">&quot;time&quot;</span>:<span class="hljs-string">&quot;2021-01-20 11:38:04&quot;</span>}
</code></pre>
</li>
</ul>
</li>
</ol>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part13/" class="navigation navigation-prev " aria-label="Previous page: Go 语言指针">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../part15/" class="navigation navigation-next " aria-label="Next page: Go 语言切片(Slice)">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 语言结构体","level":"1.15","depth":1,"next":{"title":"Go 语言切片(Slice)","level":"1.16","depth":1,"path":"part15/README.md","ref":"part15/README.md","articles":[]},"previous":{"title":"Go 语言指针","level":"1.14","depth":1,"path":"part13/README.md","ref":"part13/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part14/README.md","mtime":"2021-01-20T07:34:39.422Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

